import { readBody } from 'h3';
import { SiteModel } from '~/server/models/site.model';

// 新建站点配置，只允许一个 isUse 为 true 的配置
export default defineEventHandler(async (event): Promise<any> => {
	try {
		const body = await readBody(event);

		const { title, description, keywords, globalCss, globalJs, isUse, about } = body;

		if (!title || !description || !keywords) {
			return { code: 0, message: '所有字段不能为空' };
		}

		if (isUse) {
			// 如果新建的为 isUse true，其他全部设为 false
			await SiteModel.updateMany({ isUse: true }, { isUse: false });
		}

		await SiteModel.create({ title, description, keywords, globalCss, globalJs, isUse, about });
		return { code: 1, message: '创建成功' };
	} catch (error: any) {
		return { code: 0, message: error.message };
	}
});
